import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout/index.vue'
import Login from '@/views/Login/index.vue'
import Home from '@/views/Home/index.vue'
import Back from '@/views/Backstage/index.vue'
import NotFound from '@/components/404.vue'


import AddProduct from '@/views/BackHome/components/AddProduct.vue'
import Transaction from '@/views/BackHome/components/Stock.vue'
import Account from '@/views/BackHome/components/Account.vue'
import Stock from '@/views/BackHome/components/Stock.vue'
import Customer from '@/views/BackHome/components/Customer.vue'
import OrderList from "@/views/BackHome/components/OrderList.vue";

import Goods from '@/views/Goods/index.vue';
import Category from '@/views/Category/index.vue'
import Categorysub from '@/views/Categorysub/index.vue'

import Register from '@/views/Register/index.vue'

import Search from '@/views/Search/index.vue'

import ForgetPassword from '@/views/ForgetPassword/index.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: Layout,
      children:[
        {
          path:'',
          name:'Home',
          component:Home,
          meta: { breadcrumb: ['首页'] } // 定义 Home 页面面包屑
        },
        {
          path:':goods/:id',
          name:'Goods',
          component: Goods,
          meta: { breadcrumb: ['首页', '商品详情'] }
        },
        {
          path:'Category/:id',
          name:'Category',
          component: Category,
          meta: { breadcrumb: ['首页', '分类'] }
        },
        {
          path:'Categorysub/:id',
          name:'Categorysub',
          component: Categorysub,
          meta: { breadcrumb: ['首页', '分类', '分类商品'] }
        },
        {
          path:'Search/:content',
          name:'Search',
          component: Search
        }
        
      ]
    },
    {
      path:'/History',
      name:'History',
      component: () => import('../views/ShowTrades.vue'),
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/trades',
      name: 'trades',
      component: () => import('../views/ShowTrades.vue'),
    },
    {
      path: '/favorites',
      name: 'faorites',
      component: () => import('../views/ShowFavorites.vue'),
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/ShowCart.vue'),
    },
    {
      path: '/back',
      name: 'Back',
      component: Back,
      children: [
        {
          path: 'addProduct',
          name: 'AddProduct',
          component: AddProduct
        },
        {
          path: 'transaction',
          name: 'Transaction',
          component: Transaction
        },
        {
          path: 'account',
          name: 'Account',
          component: Account
        },
        {
          path: 'stock',
          name: 'Stock',
          component: Stock
        },
        {
          path: 'customer',
          name: 'Customer',
          component: Customer
        },
        {
          path: 'orderList',
          name: 'OderList',
          component: OrderList
        }
      ]
    },
    { path: '/:pathMatch(.*)*',
      name: 'NotFound', 
      component: NotFound 
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/forget',
      name: 'ForgetPassword',
      component: ForgetPassword
    }
  
  ],
  //跳转路由时，回滚到顶部
  scrollBehavior(to,from,savedPosition){
    if(savedPosition){
      return savedPosition;
    }else{
      return {top:0};
    }
  }
})

export default router
